using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Collections.Specialized;
using YahooAuth;

public partial class _Default : System.Web.UI.Page 
{
    private string DBConnectionString
    {
        get
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings["YahooAuthDB"].ConnectionString;
        }
    }
    //***************************************************************
    protected void Page_Load(object sender, EventArgs e)
    {
        //1- Check that there are parameters sent with the request
        if (Request.QueryString.Count != 0)
        {
            
            //2- Check if this is a SET Action
            string SetAction = "Set";
            string GetAction = "Get";
           
            if (ParameterExsit("Action", ref SetAction, true))
            {
                ProcessSetAction();
            }
            else
            {
                //3- Check if this is a Get Action
                if (ParameterExsit("Action", ref GetAction, true))
                {
                    ProcessGetAction();
                }
                else
                {
                    //4-Check if this is a Yahoo URL Call
                    ProcessYahooCall();
                }
            }
        }
        else
        {
            //Invalid call to the page return nothing
        }
    }
    //***************************************************************
    private bool ParameterExsit(string Param, ref string Value,bool CheckVal)
    {
        bool Exist = false;
        for (int i = 0; i < Request.QueryString.Keys.Count; i++)
        {
            if (Request.QueryString.Keys[i].ToLower() == Param.ToLower())
            {
                if (CheckVal)
                {
                    if (Request.QueryString[Request.QueryString.Keys[i]].ToLower() == Value.ToLower())
                    {
                        Exist = true;
                        break;
                    }
                }
                else
                {
                    Value = Request.QueryString[Request.QueryString.Keys[i]];
                    Exist = true;
                    break;
                }
            }
        }
        return Exist;
    }
    //***************************************************************
    private bool ParameterExsitInPost(string Param, ref string Value, bool CheckVal)
    {
        bool Exist = false;
        for (int i = 0; i < Request.Form.Keys.Count; i++)
        {
            if (Request.QueryString.Keys[i].ToLower() == Param.ToLower())
            {
                if (CheckVal)
                {
                    if (Request.Form[Request.Form.Keys[i]].ToLower() == Value.ToLower())
                    {
                        Exist = true;
                        break;
                    }
                }
                else
                {
                    Value = Request.Form[Request.Form.Keys[i]];
                    Exist = true;
                    break;
                }
            }
        }
        return Exist;
    }
    //***************************************************************
    private void ProcessSetAction()
    {
        YahooAuth.YahooToken Token = new YahooAuth.YahooToken();
        string EmptyVal = String.Empty;
        string val = String.Empty;
        int result = 0;
        if (ParameterExsit("appdata", ref val, false) || ParameterExsitInPost("appdata", ref val, false))
        {
            Token.Frob = val;
            Token.AddingDate = DateTime.Now;
            Token.Token = "";
            result = YahooAuth.SP.YahooToken_Insert(DBConnections.CreateConnection(this.DBConnectionString), null, ref Token);
            if (result != 0)
            {
                //Frob Saved to database
                Response.ContentType = "text/xml";
                Response.Write("<stat>Frob Created</stat>");
            }
            else
            {
                //DB error and return nothing;
            }
        }
    }
    //***************************************************************
    private void ProcessGetAction()
    {
        YahooAuth.YahooToken Token = new YahooAuth.YahooToken();
        string EmptyVal = String.Empty;
        string val = String.Empty;
        int result = 0;
        if (ParameterExsit("appdata", ref val, false) || ParameterExsitInPost("appdata", ref val, false))
        {
            Token = YahooAuth.SP.YahooToken_FindSingleByFrob(DBConnections.CreateConnection(this.DBConnectionString), val);
            string RepsonseMsg = "<token>" + Token.Token + "</token>";
            result = YahooAuth.SP.YahooToken_Delete(DBConnections.CreateConnection(this.DBConnectionString), null, Token);
            if (result != 0)
            {
                Response.ContentType = "text/xml";
                Response.Write(RepsonseMsg);
            }
            else
            {
                //Some Error happened
            }
        }
    }
    //***************************************************************
    private void ProcessYahooCall()
    {
        YahooAuth.YahooToken Token = new YahooAuth.YahooToken();
        string EmptyVal = String.Empty;
        string val = String.Empty;
        int result = 0;
 
        if (ParameterExsit("Token", ref EmptyVal, false))
        {
            if (ParameterExsit("appdata", ref val, false))
            {
                //Get Token From DB
                Token.Token = EmptyVal;
                Token.Frob = val;
                result = YahooAuth.SP.YahooToken_UpdateToken(DBConnections.CreateConnection(this.DBConnectionString), null, Token);
                if (result != 0)
                {
                    Response.ContentType = "text/html";
                    Response.Write("You may now close this browser window and return to the application to complete authentication.");
                }
                else
                {
                    //Some error happened
                }

            }
        }
        else
        {
            //Invalid Page Call and return nothing
        }
    }
}//End Class
